<template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="专题名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['subjectName', validatorRules.subjectName]"
                placeholder="请选择专题名称"
                @click="subjectNameClick"
                :disabled="classifyShowInput"
              ></a-input>
              <a-input v-decorator="['subjectId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="关键目标" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['targetName', validatorRules.targetName]"
                placeholder="请选择关键目标"
                @click="targetNameClick"
                :disabled="classifyShowInput"
              ></a-input>
              <a-input v-decorator="['targetId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="任务描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['taskDes', validatorRules.taskDes]"
                          :disabled="classifyShowInput"
                          rows="4" placeholder="请输入任务描述" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="任务状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list"
                                 v-decorator="['taskState', validatorRules.taskState]"
                                 :trigger-change="true"
                                 dictCode="pm_project_task_state"
                                 placeholder="请选择任务状态"
                                 @change='taskStateChange'
              />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="taskStateType==='3'">
            <a-form-item label="未完成原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['incompleteCause']" rows="4" placeholder="请输入未完成原因" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="预计开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-date placeholder="请选择预计开始时间"-->
<!--                      :disabled="classifyShowInput"-->
<!--                      v-decorator="['planStartTime']" :trigger-change="true" style="width: 100%" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-item label="预计结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-date placeholder="请选择预计结束时间"-->
<!--                      :disabled="classifyShowInput"-->
<!--                      v-decorator="['planEndTime']" :trigger-change="true" style="width: 100%" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-item label="责任人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['dutyPersionId']" :disabled="classifyShowInput"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="检查人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['examiner']" :disabled="classifyShowInput"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <hf-subject-select-modal ref="hfSubjectSelectModal" :multiple="false" @hf-subject-select="hfSubjectSelect"/>
      <hf-targets-select-modal ref="hfTargetsSelectModal" :multiple="false" @hf-targets-select="hfTargetsSelect"/>

    </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import HfSubjectSelectModal from './HfSubjectSelectModal'
  import HfTargetsSelectModal from './HfTargetsSelectModal'

  export default {
    name: "HfPmProjectTasksModal",
    components: {
      HfSubjectSelectModal,
      HfTargetsSelectModal
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {

          subjectName: {
            rules: [
              { required: true, message: '请选择项目专题!'},
            ]
          },
          targetName: {
            rules: [
              { required: true, message: '请选择关键目标!'},
            ]
          },
          taskDes: {
            rules: [
              { required: true, message: '请输入任务描述!'},
            ]
          },
        },
        url: {
          add: "/pm/hfPmDoubleWeekPlan/addHfPmProjectTasks",
          edit: "/pm/hfPmDoubleWeekPlan/editHfPmProjectTasks",
        }

      }
    },
    created () {
    },
    methods: {
      taskStateChange(value) {
        this.taskStateType = value
      },
      add () {
        var record = {};
        record.isEdit = false;
        this.edit(record);
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','sysOrgCode','deleteFlag','planId','subjectId','subjectName','subjectCode','targetId','targetName','targetCode','taskCode','taskDes','planStartTime','planEndTime','closeTime','taskState','dutyPersionId','remarks','incompleteCause','examiner','examinerTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['planId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      validateError(msg){
        this.$message.error(msg)
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','sysOrgCode','deleteFlag','planId','subjectId','subjectName','subjectCode','targetId','targetName','targetCode','taskCode','taskDes','planStartTime','planEndTime','closeTime','taskState','dutyPersionId','remarks','incompleteCause','examiner','examinerTime'))
      },
      subjectNameClick() {
        this.$nextTick(() => {
          this.$refs.hfSubjectSelectModal.title = '选择专题'
          this.$refs.hfSubjectSelectModal.visible = true
        })
      },
      hfSubjectSelect(data) {
        this.form.setFieldsValue({
          subjectId: data.id,
          subjectName: data.subjectName,
          subjectCode:data.subjectCode
        })
      },
      targetNameClick() {
        this.$nextTick(() => {
          this.$refs.hfTargetsSelectModal.title = '选择关键目标'
          this.$refs.hfTargetsSelectModal.visible = true
        })
      },
      hfTargetsSelect(data) {
        this.form.setFieldsValue({
          targetId: data.id,
          targetName: data.targetName,
          targetCode: data.targetCode
        })
      },

      classifyShowInput() {
        if (this.isEdit === true) {
          return true;
        }else{
          return false;
        }
      },

    }
  }
</script>
